package com.xinweisi.dataserver.module.memberdata.user;

import com.alibaba.fastjson.JSONArray;
import com.xinweisi.dataserver.constant.Enumeration;
import com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity;
import com.xinweisi.dataserver.third.RedisService;
import com.xinweisi.dataserver.util.DateUtil;
import com.xinweisi.dataserver.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * UserService
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/21 0021 16:24
 */
@Slf4j
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    private RedisService redisService;
    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * 查询当天的会员
     * @author lijie1
     * @date 2020/7/21 0021 16:44
     * @return java.util.List<com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity>
     */
    public List<UserEntity> getUserListNow(){
        return userDao.selectListNow(DateUtil.getToDayInitial());
    }

    /**
     * 查询不是当天的会员
     * @author lijie1
     * @date 2020/7/21 0021 16:45
     * @return java.util.List<com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity>
     */
    public List<UserEntity> getUserListOld(){
        List<UserEntity> entityList = new ArrayList<>();
        Map<Object, Object> map = redisService.hmget(Enumeration.USER_KEY);
        if(map==null || map.size()==0){
            List<UserEntity> list1 = userDao.selectListOld(DateUtil.getToDayInitial());
            for (UserEntity obj:list1 ) {
                String str = JsonUtils.object2json(obj);
                redisService.hset(Enumeration.USER_KEY,obj.getAccountName(),str);
                entityList.add(obj);
            }
        }else {
            for (Object obj:map.keySet()) {
                String str = map.get(obj.toString()).toString();
                entityList.add(JsonUtils.parseObject(str,UserEntity.class) );
            }
        }
        return entityList;
    }

    public int getMemberListSize() {
        return userDao.getMemberListSize();
    }

    /**
     *  处理异常的交易商添加
     * @param map
     * @author
     * @date 2020/9/16 0016 11:11
     * @return void
     */
    public void addNewUsers(Map<String, String> map) {
        for (Object obj:map.keySet()) {
            int i = 0;//userDao.selectUserByUserId(obj.toString());
            if(i==0){
                String str = map.get(obj.toString());
                JSONArray msg =JSONArray.parseArray(JSONArray.parseArray(str).getString(1));
                String userId = msg.getString(0);//会员唯一编码
                String userName = msg.getString(1);//会员名称
                long createTime = Long.parseLong(msg.get(2) + "000");//注册时间
                String loginIp = msg.getString(3);//注册ip
                Double balance = msg.getDouble(4);//初期余额
                int userType = msg.getIntValue(5);//账号类型 100 手机号 200 邮箱 300 自定义
                String businessId = msg.getString(6);//商家号
                String AppId = msg.getString(7);//注册终端
                String nickName = msg.getString(8);//昵称
                String foundType = msg.getString(9);//资金类型
                AppId = AppId.toLowerCase();
                //用户注册日志
//                MemberRegisterLogEntity memberRegisterLog = new MemberRegisterLogEntity();
//                memberRegisterLog.setUserId(userId);
//                memberRegisterLog.setUserName(userName);
//                memberRegisterLog.setCreateTime(new Date(createTime));
//                memberRegisterLog.setIp(loginIp);
//                memberRegisterLog.setBalance(balance);
//                memberRegisterLog.setUserType(userType);
//                memberRegisterLog.setBusinessId(businessId);
//                memberRegisterLog.setAppId(AppId);
//                userDao.saveMemberRegisterLog(memberRegisterLog);//存储新增会员日志

                //注册会员基本信息
                UserEntity memberInfo = new UserEntity();
                memberInfo.setAccount(userName);//账号
                memberInfo.setAccountName(userId);//唯一标识
                memberInfo.setRegisterTime(new Date(createTime));
                memberInfo.setCurAmount(balance);
                memberInfo.setAccountType(userType);
                if(userType==100){
                    memberInfo.setPhoneNumber(userName);
                }

                memberInfo.setRegisterChannel(AppId);
                memberInfo.setNickName(nickName);
                memberInfo.setFoundTypeId(foundType);
                try {
                    userDao.saveMemberInfo(memberInfo);
                }catch (Exception e){
                    System.out.println("出现并发处理会员"+memberInfo.getAccountName());
                }finally {
                    redisService.hdel1(Enumeration.NEWUSER_KEY,obj.toString());
                }
            }else {
                redisService.hdel1(Enumeration.NEWUSER_KEY,obj.toString());
            }
        }
    }
}
